package com.anbinh.doctor.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.Notification;
import com.anbinh.doctor.utils.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.squareup.picasso.Picasso;

public class MessagesAdapter extends ArrayAdapter<Notification> {
	private int resource;
	private LayoutInflater layoutInflater;
	private List<Notification> listData;
	private ImageLoader imageLoader;
	private Context mContext;

	public MessagesAdapter(Context mContext, int resource,
			List<Notification> models) {
		super(mContext, resource, models);
		this.resource = resource;
		this.listData = models;
		this.mContext = mContext;
		layoutInflater = LayoutInflater.from(mContext);

		layoutInflater = LayoutInflater.from(mContext);
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(mContext
				.getApplicationContext()));
	}

	public class ViewHolder {
		public TextView time;
		public TextView stt;
		public TextView lblName;
		public TextView lblContent;
		public TextView lblChon;
		public ImageView icon;

	}

	@Override
	public Notification getItem(int position) {
		return listData.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(resource, null, true);

			viewHolder.time = (TextView) convertView.findViewById(R.id.lbltime);
			viewHolder.lblName = (TextView) convertView
					.findViewById(R.id.lblName);
			viewHolder.lblContent = (TextView) convertView
					.findViewById(R.id.lblContent);
			viewHolder.lblChon = (TextView) convertView
					.findViewById(R.id.lblChon);

			viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Notification item = getItem(position);

		viewHolder.time.setText(item.getTime());
		viewHolder.lblName.setText(item.getSent_by_name());
		viewHolder.lblContent.setText(item.getContent());
		if (item.getStatus() == 0) {
			viewHolder.lblChon.setVisibility(View.VISIBLE);
		} else {
			viewHolder.lblChon.setVisibility(View.GONE);
		}
		Picasso.with(mContext).load(item.getSent_by_avatar_thumbs())
				.placeholder(R.drawable.icon256).resize(100, 100).centerCrop()
				.into(viewHolder.icon);

		return convertView;
	}
}
